home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / MATH / MATH1 / LEAST3.PAS < prev    next >
Pascal/Delphi Source File  |  1985-04-03  |  4KB  |  158 lines

  1. program least3;        { --> 209 }
  2. { Pascal program to perform a linear least-squares fit }
  3. { with Gauss-Jordan routine }
  4. { Sperate modules needed:
  5.             GAUSSJ,
  6.             PLOT }
  7.  
  8. const    maxr    = 20;        { data prints }
  9.     maxc    = 4;        { polynomial terms }
  10.  
  11. type    ary    = array[1..maxr] of real;
  12.     arys    = array[1..maxc] of real;
  13.     ary2    = array[1..maxr,1..maxc] of real;
  14.     ary2s    = array[1..maxc,1..maxc] of real;
  15.  
  16. var    x,y,y_calc    : ary;
  17.     resid        : ary;
  18.     coef,sig    : arys;
  19.     nrow,ncol    : integer;
  20.     correl_coef    : real;
  21.     first,done    : boolean;
  22.  
  23. external procedure cls;
  24.  
  25. procedure get_data(var x: ary;        { independant variable }
  26.            var y: ary;        { dependant variable }
  27.            var nrow: integer);    { length of vectors }
  28. { get values for n and arrays x,y }
  29.  
  30. var    i    : integer;
  31.  
  32. begin
  33.   nrow:=9;
  34.   for i:=1 to nrow do x[i]:=i;
  35.   y[1]:=2.07;    y[2]:=8.6;
  36.   y[3]:=14.42;    y[4]:=15.8;
  37.   y[5]:=18.92;    y[6]:=17.96;
  38.   y[7]:=12.98;    y[8]:=6.45;
  39.   y[9]:=0.27;
  40. end;        { proceddure get data }
  41.  
  42. procedure write_data;
  43. { print out the answers }
  44. var    i    : integer;
  45. begin
  46.   if first then first:=false else cls;
  47.   writeln;
  48.   writeln;
  49.   writeln('  I      X       Y      YCALC    RESID');
  50.   for i:=1 to nrow do
  51.     writeln(i:3,x[i]:8:1,y[i]:9:2,y_calc[i]:9:2,resid[i]:9:2);
  52.   writeln; writeln(' Coefficients errors ');
  53.   writeln(coef[1],' ',sig[1],' Constant term');
  54.   for i:=2 to ncol do
  55.     writeln(coef[i],' ',sig[i]);        { other terms }
  56.   writeln;
  57.   writeln('Correlation coefficient is ',correl_coef:8:5)
  58. end;        { write_data }
  59.  
  60. {procedure square(x: ary2;
  61.          y: ary;
  62.          var a: ary2s;
  63.          var g: arys;
  64.      nrow,ncol: integer);}
  65. { matrix multiplication routine }
  66. { a= transpose x times x }
  67. { g= y times x }
  68. {$I SQUARE.LIB }
  69.  
  70. {external procedure gaussj(var b:    ary2s;
  71.                   y:    arys;
  72.               var coef:    arys;
  73.               ncol:        integer;
  74.               var error:    boolean);
  75. }
  76. {$I GAUSSJ.LIB }
  77.  
  78. procedure linfit(x,        { independant variable }
  79.          y: ary;    { dependent variable }
  80.          var y_calc: ary;    { calculated dep. variable }
  81.          var resid:  ary;    { array of residuals }
  82.          var coef:   arys;    { coefficients }
  83.          var sig:    arys;    { error on coefficients }
  84.          nrow:       integer;    { length of array }
  85.          var ncol:   integer);    { number of terms }
  86.  
  87. { least squares fit to nrow sets of x and y pairs of points }
  88. { Seperate procedures needed:
  89.     SQUARE -> form square coefficient matrix
  90.     GAUSSJ -> Gauss-Jordan elimination }
  91.  
  92. var    xmatr        : ary2;        { data matrix }
  93.     a        : ary2s;    { coefficient matrix }
  94.     g        : arys;        { constant vector }
  95.     error        : boolean;
  96.     i,j,nm        : integer;
  97.     xi,yi,yc,srs,see,
  98.     sum_y,sum_y2    : real;
  99.  
  100. begin        { procedure linfit }
  101.   for i:=1 to nrow do
  102.     begin        { setup matrix }
  103.       xi:=x[i];
  104.       xmatr[i,1]:=1.0;    { first column }
  105.       for j:=2 to ncol do    { other columns}
  106.     xmatr[i,j]:=xmatr[i,j-1]*xi
  107.     end;
  108.   square(xmatr,y,a,g,nrow,ncol);
  109.   gaussj(a,g,coef,ncol,error);
  110.   sum_y:=0.0;
  111.   sum_y2:=0.0;
  112.   srs:=0.0;
  113.   for i:=1 to nrow do
  114.     begin
  115.       yi:=y[i];
  116.       yc:=0.0;
  117.       for j:=1 to ncol do
  118.     yc:=yc+coef[j]*xmatr[i,j];
  119.       y_calc[i]:=yc;
  120.       resid[i]:=yc-yi;
  121.       srs:=srs+sqr(resid[i]);
  122.       sum_y:=sum_y+yi;
  123.       sum_y2:=sum_y2+yi*yi
  124.     end;
  125.   correl_coef:=sqrt(1.0-srs/(sum_y2-sqr(sum_y)/nrow));
  126.   if nrow=ncol then nm:=1
  127.   else nm:=nrow-ncol;
  128.   see:=sqrt(srs/nm);
  129.   for i:=1 to ncol do        { errors on solution }
  130.     sig[i]:=see*sqrt(a[i,i])
  131. end;    { linfit }
  132.  
  133. {external procedure plot(x,y,z: ary; nrow: integer);
  134. }
  135. {$I C:PLOT.LIB }
  136.  
  137. begin        { main program }
  138.   cls;
  139.   first:=true;
  140.   done:=false;
  141.   writeln;
  142.   get_data(x,y,nrow);
  143.   repeat
  144.     repeat
  145.       write('Order of polynomial fit? ');
  146.       readln(ncol)
  147.     until ncol<5;
  148.     if ncol<1 then done:=true    { quit if ncol<1 }
  149.     else
  150.       begin
  151.     ncol:=ncol+1;        { order is one less }
  152.     linfit(x,y,y_calc,resid,coef,sig,nrow,ncol);
  153.     write_data;
  154.     plot(x,y,y_calc,nrow)
  155.     end    { else }
  156.   until done
  157. end.
  158.